import type { BaseResponse } from "@/api/response";
import { http } from "@/utils/http";
import type { ReplicaSetList } from "./types";

export class K8sReplicasets {
  replicaSetUrl = cluster_name => {
    return `/api/v1/proxy/${cluster_name}/k8s/api/v1/replicasets`;
  };

  replicaSetNamespaceUrl = (cluster_name, namespace) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/apps/v1/namespaces/${namespace}/replicasets`;
  };

  listReplicaSets(
    cluster_name?: string,
    keywords?: string,
    pageNum?: number,
    pageSize?: number
  ) {
    let url = this.replicaSetUrl(cluster_name);
    const params = {};
    if (keywords) {
      params["search"] = true;
      if (keywords) {
        params["keywords"] = keywords;
      }
      if (pageNum && pageSize) {
        params["pageNum"] = pageNum;
        params["pageSize"] = pageSize;
      }
    }
    return http.get(url, params);
  }

  listNsReplicaSets(req: {
    clusterName?: string;
    namespace?: string;
    labelSelector?: string;
    fieldSelector?: string;
  }): BaseResponse<ReplicaSetList> {
    return http.get(
      `${this.replicaSetNamespaceUrl(req.clusterName, req.namespace)}`
    );
  }

  replicaWorkloadSetNamespaceUrl = (cluster_name, namespace, selector) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/apps/v1/namespaces/${namespace}/replicasets?labelSelector=${selector}&`;
  };

  listNsReplicaSetsWorkload(cluster_name, namespace, selector) {
    return http.get(
      `${this.replicaWorkloadSetNamespaceUrl(cluster_name, namespace, selector)}`
    );
  }
}
